package com.ljx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.generator.config.IFileCreate;
import com.ljx.R;
import com.ljx.exception.GuliException;
import com.ljx.pojo.EduTeacher;
import com.ljx.pojo.vo.TeacherQuery;
import com.ljx.service.impl.EduTeacherServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 李加喜
 * @since 2020-09-22
 */
@RestController
@RequestMapping("/eduservice")
@Api(description = "讲师管理")
@CrossOrigin //解决跨域问题  因为在网关里面配置了处理关于的方法 所以这里在在使用就会出现错误
public class EduTeacherController {
    @Autowired
    private EduTeacherServiceImpl eduTeacherService;

    //查询所有的数据
    @ApiOperation("查询所有讲师")
    @GetMapping("/findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }
    //逻辑删除
    @DeleteMapping("/delete/{id}")
    @ApiOperation("逻辑删除讲师")
    public R deleteById(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable("id") String id){
        boolean flag = eduTeacherService.removeById(id);
        if (flag){
            return R.ok();
        }
        return R.error();
    }
    //实现分页查询讲师的方法
    @ApiOperation("分页查询讲师信息")
    @GetMapping("/pageTeacher/{current}/{info}")
    public R pageListTeacher(@PathVariable Long current,
                             @PathVariable Long info){
        Page<EduTeacher> page = new Page<EduTeacher>(current, info);
        eduTeacherService.page(page, null);
        //此时的Page里面包含了所有的数据
        long total = page.getTotal();//活动总记录数
        List<EduTeacher> records = page.getRecords();
        //获取list集合显示全部的数据
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total",total);
        hashMap.put("records",records);
        return R.ok().data(hashMap);
    }
    //多条件组合查询分页功能 (重点)
    @ApiOperation("多条件分页查询")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageListTeacherCondition(@PathVariable Long current,
                                      @PathVariable Long limit,
                                      @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<EduTeacher> page = new Page<>(current, limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //构建多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //使用的是模糊查询
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }
        //对创建时间进行排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现查询分页
        eduTeacherService.page(page,wrapper);
        long total = page.getTotal();
        List<EduTeacher> list = page.getRecords();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("total",total);
        hashMap.put("list",list);
        return R.ok().data(hashMap);
    }
    //添加讲师的信息
    @ApiOperation("添加讲师的信息")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody(required = false) EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }
        return R.error();
    }
    //根据讲师的ID进行查询  为了修改的时候进行数据回显
    @ApiOperation("根据讲师的ID查询讲师的信息")
    @GetMapping("/getTeacher/{id}")
    public R selectById(@PathVariable Long id){
        EduTeacher teacher = eduTeacherService.getById(id);
        if (teacher!=null){
            return R.ok().data("teacher",teacher);
        }
        return R.error();
    }
    //讲师修改
    @ApiOperation("修改讲师的信息")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody(required = false) EduTeacher eduTeacher){
//        try {
//            int a=10/0;
//        } catch (Exception e) {
//            throw new GuliException(666,"执行了自定义的异常处理");
//        }
        boolean flag = eduTeacherService.updateById(eduTeacher);
          //出现错误的时候就会执行全局定义的异常处理
        if (flag){
            return R.ok();
        }
        return R.error();
    }
}

